﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterpreterPattern
{
    /// <summary>
    /// 0
    /// 0
    /// </summary>
    public class LastExpression : BaseExpression
    {
        private static Dictionary<char, string> TransDictionary = new Dictionary<char, string>();

        /// <summary>
        /// 静态构造函数：由CLR保障，在程序第一次使用该类型前执行，而且只执行一次
        /// </summary>
        static LastExpression()
        {
            TransDictionary.Add('1', "1");
            TransDictionary.Add('2', "2");
            TransDictionary.Add('3', "3");
            TransDictionary.Add('4', "4");
            TransDictionary.Add('5', "5");
            TransDictionary.Add('6', "6");
            TransDictionary.Add('7', "7");
            TransDictionary.Add('8', "8");
            TransDictionary.Add('9', "9");
        }

        public override void Trans(Context context)
        {
            string word = context.Word;
            if (string.IsNullOrWhiteSpace(word)) return;
            List<string> wordList = new List<string>();

            foreach (var item in word.ToCharArray())
            {
                if (TransDictionary.ContainsKey(item))
                {
                    wordList.Add(TransDictionary[item]);
                }
                else
                {
                    wordList.Add("0");
                }
            }
            context.SetResult(string.Concat(wordList));
        }
    }
}
